import Vue from 'vue'
import * as dayjs from 'dayjs'

Vue.filter('NumberFormat', function (value) {
  if (!value) {
    return '0'
  }
  let intPartFormat = value.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') //将整数部分逢三一断
  return intPartFormat
})

Vue.filter('dayjs', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return dayjs(dataStr).format(pattern)
})

Vue.filter('moment', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return dayjs(dataStr).format(pattern)
})

/** 字符串超长截取省略号显示 */
Vue.filter('ellipsis', function (value, vlength = 25) {
  if (!value) {
    return ''
  }
  // console.log('vlength: '+ vlength);
  if (value.length > vlength) {
    return value.slice(0, vlength) + '...'
  }
  return value
})

/* 根据值取出对应条的字段或者数组（单选或者多选） */
Vue.filter('filterArray', function (value, datas, inField, outField) {
  const values = value!=='' && value!==null && value!==undefined && Array.isArray(value)?
        value: value!=='' && value!==null && value!==undefined && !Array.isArray(value)?
        value.toString().split(',') : []
  const filterDatas = inField && [...datas].filter(x => [...values].some(y => y == x[inField])) || datas
  const outFieldMap = outField && filterDatas.length>0 && filterDatas.map(v=>v[outField]).filter(obj => obj !== undefined) || []
  const outFieldValue = outField && outFieldMap.length>0 ? outFieldMap.toString(): ''
  return outField? outFieldValue: filterDatas
})
